package com.zsule.controllers.manager;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.zsule.controllers.BaseController;
import com.zsule.entity.EShop;
import com.zsule.entity.PageEntity;
import com.zsule.entity.ShopCert;
import com.zsule.entity.SmokeCert;
import com.zsule.service.IShopCertService;
import com.zsule.service.IShopService;
import com.zsule.service.ISmokeCertService;

@Controller
@RequestMapping("/manager/cert")
public class CertVerifyController extends BaseController {
	private static final Logger logger = Logger.getLogger(ManagerController.class);
	@Autowired
	private ISmokeCertService smokeCertService;
	@Autowired
	private IShopCertService shopCertService;
	@Autowired
	private IShopService eshopService;

	private static final String SMOKE_LIST = "/manager/cert/smoke_list.httl";
	private static final String SMOKE_VERIFY = "/manager/cert/smoke_verify.httl";
	private static final String SHOP_LIST = "/manager/cert/shop_list.httl";
	private static final String SHOP_VERIFY = "/manager/cert/shop_verify.httl";

	@RequestMapping("/smoke_list")
	public ModelAndView smokeList(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("page") PageEntity page) {
		ModelAndView modelAndView = new ModelAndView(SMOKE_LIST);
		try {
			this.setPage(page);
			this.getPage().setPageSize(20);
			List<SmokeCert> list = smokeCertService.findList(this.getPage());
			modelAndView.addObject("smokeList", list);
			modelAndView.addObject("page", this.getPage());
		} catch (Exception e) {
			logger.error("CertVerifyController.smokeList", e);
		}
		return modelAndView;
	}
	@RequestMapping(value = "/smoke_verify", method = RequestMethod.GET)
	public ModelAndView smokeVerify(String id) {
		ModelAndView modelAndView = new ModelAndView(SMOKE_VERIFY);
		try {
			SmokeCert cert = smokeCertService.findByUserId(id);
			EShop shop = eshopService.findById(id);
			modelAndView.addObject("cert",cert);
			modelAndView.addObject("shop",shop);
		} catch (Exception e) {
			logger.error("CertVerifyController.smokeVerify", e);
		}
		return modelAndView;
	}

	@RequestMapping(value = "/smoke_verify", method = RequestMethod.POST)
	public RedirectView smoke_verify(SmokeCert cert, HttpServletRequest request) {
		try {
			SmokeCert old = smokeCertService.findByUserId(cert.getUserId());
			EShop shop = eshopService.findById(cert.getUserId());
			if(!cert.getStatus().equals(SmokeCert.CROSS)){
				shop.setIsSmokeCert(true);
				eshopService.save(shop);
			}
			old.setVerifyDate(new Date(System.currentTimeMillis()));
			old.setStatus(cert.getStatus());
			smokeCertService.save(old);
		} catch (Exception e) {
			logger.error("CertVerifyController.smoke_verify", e);
		}
		return new RedirectView("/manager/cert/smoke_list");
	}
	@RequestMapping("/shop_list")
	public ModelAndView shopList(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("page") PageEntity page) {
		ModelAndView modelAndView = new ModelAndView(SHOP_LIST);
		try {
			this.setPage(page);
			this.getPage().setPageSize(20);
			List<ShopCert> list = shopCertService.findList(this.getPage());
			modelAndView.addObject("shopList", list);
			modelAndView.addObject("page", this.getPage());
		} catch (Exception e) {
			logger.error("CertVerifyController.shopList", e);
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "/shop_verify", method = RequestMethod.GET)
	public ModelAndView shopVerify(String id) {
		ModelAndView modelAndView = new ModelAndView(SHOP_VERIFY);
		try {
			ShopCert cert = shopCertService.findByUserId(id);
			EShop shop = eshopService.findById(id);
			modelAndView.addObject("cert",cert);
			modelAndView.addObject("shop",shop);
		} catch (Exception e) {
			logger.error("CertVerifyController.shopVerify", e);
		}
		return modelAndView;
	}

	@RequestMapping(value = "/shop_verify", method = RequestMethod.POST)
	public RedirectView shop_verify(ShopCert cert, HttpServletRequest request) {
		try {
			ShopCert old = shopCertService.findByUserId(cert.getUserId());
			EShop shop = eshopService.findById(cert.getUserId());
			if(!cert.getStatus().equals(ShopCert.CROSS)){
				shop.setIsBusinessCert(true);
				eshopService.save(shop);
			}
			old.setVerifyDate(new Date(System.currentTimeMillis()));
			old.setStatus(cert.getStatus());
			shopCertService.save(old);
		} catch (Exception e) {
			logger.error("CertVerifyController.shop_verify", e);
		}
		return new RedirectView("/manager/cert/shop_list");
	}
}
